package org.hong.monkey.scheduler

import org.hong.monkey.executor.ExecutorExitCode

/**
 * Represents an explanation for a executor or whole slave failing or exiting.
 */
private[monkey]
class ExecutorLossReason(val message: String) extends Serializable {
  override def toString: String = message
}

private[monkey]
case class ExecutorExited(exitCode: Int, exitCausedByApp: Boolean, reason: String)
  extends ExecutorLossReason(reason)

private[monkey] object ExecutorExited {
  def apply(exitCode: Int, exitCausedByApp: Boolean): ExecutorExited = {
    ExecutorExited(
      exitCode,
      exitCausedByApp,
      ExecutorExitCode.explainExitCode(exitCode))
  }
}

private[monkey] object ExecutorKilled extends ExecutorLossReason("Executor killed by driver.")

/**
 * A loss reason that means we don't yet know why the executor exited.
 *
 * This is used by the task scheduler to remove state associated with the executor, but
 * not yet fail any tasks that were running in the executor before the real loss reason
 * is known.
 */
private [monkey] object LossReasonPending extends ExecutorLossReason("Pending loss reason.")

private[monkey]
case class SlaveLost(_message: String = "Slave lost")
  extends ExecutorLossReason(_message)
